<template>
  <div>
    <el-card class="cardbox">
      <pagetool>
        <template #before>
          <el-button type="primary" size="small" @click="add">添加分类</el-button>
        </template>
        <template #after>
          <el-input v-model="value" placeholder="请输入关键字" class="input-with-select">
            <el-button slot="append" icon="el-icon-search" @click="search" />
          </el-input>
        </template>
      </pagetool>
      <el-divider />
      <!-- 表格 -->
      <el-table border class="table" :data="tableData">
        <el-table-column
          prop="image"
          label="图片"
          align="center"
        >
          <template v-slot="scope">
            <img :src="scope.row.image" alt="" style="width:200px;height:200px;object-fit: cover;">
          </template>
        </el-table-column>
        <el-table-column
          prop="title"
          label="标题"
          align="center"
        />
        <el-table-column
          prop="desc"
          label="介绍"
          align="center"
        />
        <el-table-column
          label="操作"
          align="center"
        >
          <template v-slot="scope">
            <el-button type="primary" size="small" @click="edit(scope.row.id)">编辑</el-button>
            <el-button type="danger" size="small" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-divider />
      <!-- 分页 -->
      <div class="pag">
        <el-pagination
          layout="prev, pager, next"
          :total="total"
          :page-size="pageConfig.limit"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <!-- 弹框 -->
    <Dialog
      ref="Form"
      :show-dialog="showDialog"
      @close="close"
      @reload="reload"
    />
  </div>
</template>

<script>
import { classiFicationList, getDetail, delclassiFication, getCount } from '@/api/classification'
// 弹框
import Dialog from './components/dialog.vue'

export default {
  components: {
    Dialog
  },
  data() {
    return {
      tableData: [],
      pageConfig: {
        limit: 2,
        start: 0,
        title: ''
      },
      total: 0,
      showDialog: false,
      value: ''
    }
  },
  created() {
    this.getList()
    this.getconut()
  },
  methods: {
    async getList() {
      const res = await classiFicationList(this.pageConfig)
      // console.log(res)
      this.tableData = res
    },
    // 分类数量
    async getconut() {
      const res = await getCount(this.pageConfig)
      // console.log(res)
      this.total = res
    },
    // 页码改变
    pageChange(page) {
      // console.log(page)
      this.pageConfig.start = (page - 1) * 2
      this.getList()
    },
    close() {
      this.showDialog = false
    },
    reload() {
      this.getList()
      this.getconut()
    },
    // 新增
    add() {
      this.showDialog = true
    },
    // 编辑
    async edit(id) {
      // console.log(id)
      const res = await getDetail(id)
      console.log(res)
      this.$refs.Form.dialogForm = res
      this.showDialog = true
    },
    // 删除
    async del(id) {
      // console.log(id)
      try {
        await this.$confirm('是否确认删除')
        await delclassiFication(id)
        // 删除最后一条数据加载上一页
        if (this.pageConfig.start > 1 && this.tableData.length === 1) {
          this.pageConfig.start -= 2
        }
        this.getList()
        this.getconut()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 搜索
    search() {
      console.log(this.value)
      this.pageConfig.title = this.value
      this.getList()
      this.getconut()
      this.value = ''
    }
  }
}
</script>

<style scoped>
.cardbox{
  margin: 20px;
}
.table{
  margin-top: 20px;
  margin-bottom: -30px;
}
.pag{
  text-align: center;
}
::v-deep .el-divider--horizontal{
  margin-bottom: 20px;
}
</style>
